package controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strconv"
)

type SystemCtl struct {
	db *gorm.DB
}

var System = SystemCtl{}

func (ctl *SystemCtl) GetLogList(c *gin.Context) {
	ctl.db = models.Db
	var logList []models.Log
	page := c.Query("page")
	limit := c.Query("limit")
	//	获取范围内的数据
	limitInt, _ := strconv.Atoi(limit)
	pageInt, _ := strconv.Atoi(page)
	//倒序获取
	ctl.db.Order("id desc").Limit(limitInt).Offset((pageInt - 1) * limitInt).Find(&logList)
	tools.SuccessResp(c, "获取成功", logList)
}

// GetUserList 用户的增删改查
func (ctl *SystemCtl) GetUserList(c *gin.Context) {
	ctl.db = models.Db
	var userList []models.User
	ctl.db.Preload("Role.Right").Find(&userList)
	tools.SuccessResp(c, "获取成功", userList)
}

func (ctl *SystemCtl) AddUser(c *gin.Context) {
	ctl.db = models.Db
	var user models.User
	c.ShouldBind(&user)
	ctl.db.Create(&user)
	tools.SuccessResp(c, "添加成功", nil)
}

func (ctl *SystemCtl) UpdateUser(c *gin.Context) {
	ctl.db = models.Db
	var user models.User
	c.ShouldBind(&user)
	ctl.db.Model(&user).Association("Role").Replace(user.Role)
	ctl.db.Save(&user)
	tools.SuccessResp(c, "修改成功", nil)
}

func (ctl *SystemCtl) DelUser(c *gin.Context) {
	ctl.db = models.Db
	var user models.User
	c.ShouldBind(&user)
	ctl.db.Select("Role").Delete(&user)
	tools.SuccessResp(c, "删除成功", nil)
}

func (ctl *SystemCtl) GetRoleList(c *gin.Context) {
	ctl.db = models.Db
	var roleList []models.Role
	ctl.db.Preload("Right").Find(&roleList)
	tools.SuccessResp(c, "获取成功", roleList)
}

func (ctl *SystemCtl) AddRole(c *gin.Context) {
	ctl.db = models.Db
	var role models.Role
	c.ShouldBind(&role)
	ctl.db.Create(&role)
	tools.SuccessResp(c, "添加成功", nil)
}

func (ctl *SystemCtl) UpdateRole(c *gin.Context) {
	var err error
	ctl.db = models.Db
	var role models.Role
	c.ShouldBind(&role)
	//ctl.db.Model(&role).Association("Right").Replace(role.Right)
	//ctl.db.Updates(&role)
	err = ctl.db.Transaction(func(tx *gorm.DB) error {
		if err = tx.Updates(&role).Error; err != nil {
			return err
		}
		if err = tx.Model(&role).Association("Right").Replace(role.Right); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		tools.ErrorResp(c, 500, "修改失败")
		return
	}
	tools.SuccessResp(c, "修改成功", nil)
}

func (ctl *SystemCtl) DelRole(c *gin.Context) {
	var err error
	ctl.db = models.Db
	var role models.Role
	c.ShouldBind(&role)
	//ctl.db.Select("Right").Delete(&role)
	err = ctl.db.Select("Right").Delete(&role).Error
	if err != nil {
		tools.ErrorResp(c, 500, "删除失败")
		return
	}
	tools.SuccessResp(c, "删除成功", nil)
}

func (ctl *SystemCtl) GetRightList(c *gin.Context) {
	ctl.db = models.Db
	var rightList []models.Right
	ctl.db.Find(&rightList)
	tools.SuccessResp(c, "获取成功", rightList)
}

func (ctl *SystemCtl) AddRight(c *gin.Context) {
	ctl.db = models.Db
	var right models.Right
	c.ShouldBind(&right)
	ctl.db.Create(&right)
	tools.SuccessResp(c, "添加成功", nil)
}

func (ctl *SystemCtl) UpdateRight(c *gin.Context) {
	ctl.db = models.Db
	var right models.Right
	c.ShouldBind(&right)
	ctl.db.Save(&right)
	tools.SuccessResp(c, "修改成功", nil)
}

func (ctl *SystemCtl) DelRight(c *gin.Context) {
	var err error
	ctl.db = models.Db
	var right models.Right
	c.ShouldBind(&right)
	err = ctl.db.Delete(&right).Error
	if err != nil {
		tools.ErrorResp(c, 500, "删除失败")
		return
	}
	tools.SuccessResp(c, "删除成功", nil)
}

func (ctl *SystemCtl) SubSetting(c *gin.Context) {
	ctl.db = models.Db
	var setting models.Settting
	c.ShouldBind(&setting)
	ctl.db.Model(&models.Settting{}).Where("id = ?", 1).Updates(setting)
	tools.SuccessResp(c, "修改成功", nil)
}
